package com.car.loan.project.cm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.controller.BaseController;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cm.domain.CmInsuranceCompany;
import com.car.loan.project.cm.domain.CmSales;
import com.car.loan.project.cm.service.ICmSalesService;
import com.car.loan.project.cm.util.bo.CmSalesBo;
import com.car.loan.project.cm.util.bo.InsuranceBaseBo;
import com.car.loan.project.cm.util.enunms.DealerStatus;
import com.car.loan.project.cm.util.select.InsuranceBaseSelect;
import com.car.loan.project.cm.util.select.SalesBaseSelect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/10/22 21:47
 */
@RestController
@RequestMapping("/cm/sales")
@Api(value = "销货单位管理", tags = "基础数据-销货单位管理接口")
public class CmSalesController extends BaseController {

    @Autowired
    private ICmSalesService cmSalesService;

    @PreAuthorize("@ss.hasPermi('cm:sales:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取销货单位列表", responseContainer = "list", response = CmSales.class)
    public AjaxResult list(@Valid SalesBaseSelect salesBaseSelect) {
        IPage<CmSales> page = new Page<>(salesBaseSelect.getPageNum(),
            salesBaseSelect.getPageSize());

        CmSales cmSales = new CmSales();
        if (null != salesBaseSelect.getStatus()) {
            cmSales.setStatus(salesBaseSelect.getStatus());
        }
        QueryWrapper<CmSales> queryWrapper = new QueryWrapper<>(cmSales);
        if (StringUtils.isNotEmpty(salesBaseSelect.getName())) {
            queryWrapper.like("name", salesBaseSelect.getName());
        }
        return AjaxResult.success(cmSalesService.selectPage(page, queryWrapper));
    }


    @PreAuthorize("@ss.hasPermi('cm:sales:query')")
    @GetMapping(value = "/{salesId}")
    @ApiOperation(value = "获取销货单位详情", response = CmSales.class)
    public AjaxResult getInfo(@PathVariable Long salesId) {
        return AjaxResult
            .success(cmSalesService.selectCmSalesById(salesId));
    }


    @PreAuthorize("@ss.hasPermi('cm:sales:add')")
    @Log(title = "销货单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加销货单位")
    public AjaxResult add(
        @ApiParam(value = "名称", required = true) @RequestParam("name") String name) {
        if (!cmSalesService.checkNameUnique(name, 0L)) {
            return AjaxResult.error("添加失败，该销货单位名称已存在");
        }
        CmSales cmSales = new CmSales();
        cmSales.setName(name);
        cmSales.setStatus(DealerStatus.NORMAL.status);
        return toAjax(cmSalesService.insertCmSales(cmSales));
    }

    @PreAuthorize("@ss.hasPermi('cm:sales:edit')")
    @Log(title = "销货单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改销货单位")
    public AjaxResult edit(@RequestBody @Valid CmSalesBo cmSalesBo) {
        if (!cmSalesService.checkNameUnique(cmSalesBo.getName(), 0L)) {
            return AjaxResult.error("添加失败，该销货单位名称已存在");
        }
        CmSales sales = new CmSales();
        sales.setId(cmSalesBo.getId());
        sales.setName(cmSalesBo.getName());
        sales.setStatus(cmSalesBo.getStatus());
        cmSalesService.updateCmSales(sales);
        return AjaxResult.success();
    }
}
